package com.controller;

import com.dao.*;
import com.po.FriendList;
import com.po.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet({"/modifName","/modifPassword","/modify/EnvelopeName","/modify/deleteNote"})
public class modifSystem extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        HttpSession session = request.getSession(false);
        if(session!=null && session.getAttribute("id")!=null){
            if("/modifName".equals(servletPath)){
                doModifName(request,response);
            }else if("modifPassword".equals(servletPath)){
                doModifPassword(request,response);
            }else if("/modify/EnvelopeName".equals(servletPath)){
                doModifyEnvelopeName(request,response);
            }else if("/modify/deleteNote".equals(servletPath)){
                doDeleteNote(request,response);
            }
        }else{
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
    }

    private void doDeleteNote(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("UTF-8");
        int note_id = Integer.parseInt(request.getParameter("note_id"));
        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String envelope_name = request.getParameter("envelope_name");

        NoteDao.deleteNote(note_id);
        CommentDao.deleteComment(note_id);
        NoteDao.deleteLikeNote(note_id);

        response.sendRedirect(request.getContextPath()+"/function/selectNote?envelope_id="+envelope_id+"&envelope_name="+envelope_name);
    }

    private void doModifyEnvelopeName(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        String envelopeName = request.getParameter("envelopeName");
        if(envelopeName.length()>=1){
            if(envelopeName.length()>=10){
                out.print("输入的名字过长，请重新输入");
            }else{
                EnvelopeDao.modifyEnvelopeName(envelope_id,envelopeName);
                response.sendRedirect(request.getContextPath()+"/function/myEnvelope");
            }
        }else{
            out.print("输入的名字不能为空");
        }
    }

    private void doModifPassword(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");
        String password = request.getParameter("password");
        UserDao.updatePassword(id,password);
        out.print("修改成功");
    }

    private void doModifName(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");
        String netName = request.getParameter("netName");
        User user = FriendList.getUser(id);
        if(netName.equals(user.getName())){
            out.print("您的名字没有改变");
        }else{
            UserDao.updateName(netName,id);
            out.print("修改成功");
        }
    }
}
